package com.cungmua.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.model.Category;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kelvin.nguyen on 6/19/14.
 */
public class MenuAdapter extends BaseAdapter{
    //    ================Drawer menu =======

    public static final int ICON_DRAW_FASHION=R.drawable.icon_draw_fashion;
    public static final int ICON_DRAW_FASHION_ACCESSORIES=R.drawable.icon_draw_fashion_accessories;
    public static final int ICON_DRAW_FOOD_RESTAURANT=R.drawable.icon_draw_food_restaurant;
    public static final int ICON_DRAW_HEALTH_BEAUTY=R.drawable.icon_draw_health_beauty;
    public static final int ICON_DRAW_TRAVEL_RESORT=R.drawable.icon_draw_travel_resort;
    public static final int ICON_DRAW_ENTERTAIMENT_EDUCATION=R.drawable.icon_draw_entertaiment_education;
    public static final int ICON_DRAW_HOUSEHOLD_FURNITURE=R.drawable.icon_draw_household_furniture;
    public static final int ICON_DRAW_ELECTRONIC_TECHNOLOGY=R.drawable.icon_draw_electronic_technology;
    public static final int ICON_DRAW_MOTHER_BABY=R.drawable.icon_draw_mother_baby;

    public static final int ICON_DRAW_HOME=R.drawable.icon_draw_home;
    public static final int ICON_DRAW_HOME_PROMOTIONS=R.drawable.icon_draw_home_promotions;
    public static final int ICON_DRAW_LOGOUT=R.drawable.icon_draw_logout;
    public static final int ICON__DRAW_LOGIN=R.drawable.icon__draw_login;
    public static final int ICON__DRAW_REGISTER=R.drawable.icon__draw_register;
    public static final int ICON__DRAW_INFO=R.drawable.icon__draw_login;

    public static final int ICON_DRAW_LOVE=R.drawable.icon_draw_love;
    public static final int ICON_DRAW_FORGOTPASS=R.drawable.icon_draw_forgotpass;
    public static final int ICON_DRAW_MONEY=R.drawable.icon_draw_money;
    public static final int ICON_DRAW_HISTORY=R.drawable.icon_draw_history;
    public static final int ICON_DRAW_LOCATIONS=R.drawable.cm_position;
    public static final int VISIBLE=View.VISIBLE;
    public static final int GONE=View.GONE;

    private final Context context;
    private final LayoutInflater inflater;
    private List<Category> categories;
    private boolean isSub=false;


    public MenuAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.categories = new ArrayList<Category>();
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public boolean isSub() {
        return isSub;
    }

    public void setSub(boolean isSub) {
        this.isSub = isSub;
    }

    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    @Override
    public int getCount() {
        return categories.size();
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Category getItem(int position) {
        return categories.get(position);
    }

    /**
     * Get the row id associated with the specified position in the list.
     *
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position    The position of the item within the adapter's data set of the item whose view
     *                    we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *                    is non-null and of an appropriate type before using. If it is not possible to convert
     *                    this view to display the correct data, this method can create a new view.
     *                    Heterogeneous lists can specify their number of view types, so that this View is
     *                    always of the right type (see {@link #getViewTypeCount()} and
     *                    {@link #getItemViewType(int)}).
     * @param parent      The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        Category category = categories.get(position);

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.row, parent, false);

            holder.txtTitle = (TextView) convertView.findViewById(R.id.row_title);
            holder.iconList = (ImageView) convertView.findViewById(R.id.row_icon);
            holder.icon_next = (ImageView) convertView.findViewById(R.id.icon_next);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtTitle.setText(category.getName());
        holder.iconList.setImageResource(category.getIconLeft());
        holder.txtTitle.setBackgroundResource(0);
        holder.icon_next.setVisibility(VISIBLE);
        holder.iconList.setVisibility(VISIBLE);
        convertView.setBackgroundColor(Color.TRANSPARENT);
        holder.txtTitle.setTextColor(context.getResources().getColor(R.color.text_menu_color));

        holder.txtTitle.setTypeface(null, Typeface.NORMAL);

        int icon_draw=0;

        switch (category.getId()) {
            case 1:
                icon_draw=ICON_DRAW_HOME;
                holder.icon_next.setVisibility(GONE);
                break;
            case 2:
                icon_draw=ICON_DRAW_HOME_PROMOTIONS;
                holder.icon_next.setVisibility(GONE);
                break;
            case 33333:
                holder.icon_next.setVisibility(GONE);
                holder.iconList.setVisibility(View.INVISIBLE);
                convertView.setBackgroundColor(Color.parseColor("#dddddd"));
                holder.txtTitle.setTypeface(null, Typeface.BOLD);
                break;
            case 11111:
                holder.icon_next.setVisibility(GONE);
                holder.iconList.setVisibility(View.INVISIBLE);
                convertView.setBackgroundColor(Color.parseColor("#dddddd"));
                holder.txtTitle.setTypeface(null, Typeface.BOLD);
                break;
            case 1212121212:
                icon_draw=ICON__DRAW_INFO;
                break;
            case 1313131313:
                icon_draw=ICON__DRAW_REGISTER;
                break;
            case 1515151515:
                holder.icon_next.setVisibility(GONE);
                holder.iconList.setVisibility(View.INVISIBLE);
                convertView.setBackgroundColor(Color.parseColor("#dddddd"));
                holder.txtTitle.setTypeface(null, Typeface.BOLD);
                break;
            case 1616161616:
                icon_draw=ICON_DRAW_LOCATIONS;
                break;
            case 1717171717:
                holder.icon_next.setVisibility(GONE);
                holder.iconList.setVisibility(View.GONE);
                holder.txtTitle.setText("");
                holder.txtTitle.setBackgroundResource(R.drawable.btn_login);
                convertView.setBackgroundColor(context.getResources().getColor(R.color.bg_login));
                break;
            case 56:
                icon_draw=ICON_DRAW_FASHION;
                break;
            case 179:
                icon_draw=ICON_DRAW_FASHION_ACCESSORIES;
                break;
            case 53:
                icon_draw=ICON_DRAW_FOOD_RESTAURANT;
                break;
            case 54:
                icon_draw=ICON_DRAW_HEALTH_BEAUTY;
                break;
            case 52:
                icon_draw=ICON_DRAW_TRAVEL_RESORT;
                break;
            case 55:
                icon_draw=ICON_DRAW_ENTERTAIMENT_EDUCATION;
                break;
            case 57:
                icon_draw=ICON_DRAW_HOUSEHOLD_FURNITURE;
                break;
            case 59:
                icon_draw=ICON_DRAW_ELECTRONIC_TECHNOLOGY;
                break;
            case 68:
                icon_draw=ICON_DRAW_MOTHER_BABY;
                break;
        }
        if(isSub ) {
            holder.iconList.setImageResource(R.drawable.dot);
            holder.icon_next.setVisibility(GONE);
        }else{
            holder.iconList.setImageResource(icon_draw);

        }
        if(category.getIconLeft()==0){
            holder.iconList.setImageResource(R.drawable.dot);
        }
        if(category.getId()==1616161616)
            holder.iconList.setImageResource(R.drawable.cm_position);

        return convertView;
    }

    private class ViewHolder {
        public TextView txtTitle;
        public ImageView iconList;
        public ImageView icon_next;

    }

}
